<template>
  <div class="container">
    <view class="box">
      <view class="fz14 bold title">基本资料</view>
      <uv-cell-group :border="false">
        <uv-cell title="昵称" :border="false">
          <!-- 自定义右侧值 -->
          <template v-slot:value>
            <uv-input placeholder="请输入昵称" v-model="model.nickName" inputAlign="right" border="none"></uv-input>
          </template>
        </uv-cell>
        <uv-cell title="出生日期" :value="birthday_format" isLink :border="false" @click="instance.refs.selectBirthdayRef.open()"></uv-cell>
        <uv-cell title="性别" :value="sex_format" isLink :border="false" @click="instance.refs.selectSexRef.open()"></uv-cell>
        <uv-cell title="身高" :value="height_format" isLink :border="false" @click="instance.refs.selectHeightRef.open()"></uv-cell>
        <uv-cell title="体重" :value="weight_format" isLink :border="false" @click="instance.refs.selectWeightRef.open()"></uv-cell>
        <uv-cell title="现居住地" :value="`${model.city}${model.district}`" isLink :border="false" @click="chooseLocation"></uv-cell>
        <uv-cell title="家乡" :value="`${model.homeEconomize}${model.homeMarket}${model.homeCounty}`" isLink :border="false" @click="instance.refs.selectAddressRef.open()"></uv-cell>
        <uv-cell title="婚姻情况" :value="model.marriage==1?'已婚':'未婚'" isLink :border="false" @click="instance.refs.selectMarriageRef.open()"></uv-cell>
      </uv-cell-group>
    </view>

    <view class="box">
      <view class="fz14 bold title">教育资料</view>
      <uv-cell-group :border="false">
        <uv-cell title="最高学历" :value="model.edu" isLink :border="false" @click="instance.refs.selectDictRef.open()"></uv-cell>
        <uv-cell title="毕业院校" :border="false">
          <!-- 自定义右侧值 -->
          <template v-slot:value>
            <view class="positionRelative">
              <uv-input placeholder="请输入毕业院校" v-model="model.school" inputAlign="right" border="none" @input="searchSchool"></uv-input>
              <view class="school-list textRight">
                <view class="school fz12" @click="schoolClick(item)" v-for="(item, index) in schools" :key="index">{{item.name}}</view>
              </view>
            </view>
          </template>
        </uv-cell>
        <uv-cell title="职位" :border="false">
          <!-- 自定义右侧值 -->
          <template v-slot:value>
            <uv-input placeholder="请输入职位" v-model="model.industry" inputAlign="right" border="none"></uv-input>
          </template>
        </uv-cell>
        <uv-cell title="公司" :value="model.foreignCompanyName" :border="false"></uv-cell>
        <uv-cell title="年收入" :value="model.income" isLink :border="false" @click="instance.refs.selectSalaryRef.open()"></uv-cell>
      </uv-cell-group>

      <view class="actionBtn" @click="save">
        <uv-button text="保存" color="#9E06FF" shape="circle" :loading="loading"></uv-button>
      </view>
    </view>

    <selectBirthday v-model:birthday="model.birthday" v-model:constellation="model.constellation" ref="selectBirthdayRef"></selectBirthday>

    <selectSex ref="selectSexRef" v-model="model.sex"></selectSex>

    <selectHeight ref="selectHeightRef" v-model="model.height"></selectHeight>

    <selectWeight ref="selectWeightRef" v-model="model.weight"></selectWeight>

    <selectAddress ref="selectAddressRef" v-model:province="model.homeEconomize" v-model:city="model.homeMarket" v-model:district="model.homeCounty"></selectAddress>

    <selectMarriage ref="selectMarriageRef" v-model="model.marriage"></selectMarriage>

    <selectDict dict="education_value" ref="selectDictRef" v-model="model.edu"></selectDict>

    <selectSalary dict="income_value" ref="selectSalaryRef" v-model="model.income"></selectSalary>
  </div>
</template>

<script setup lang="ts">
//薪酬
import selectSalary from "@/packagePages/my/completeInfo/components/selectDict.vue";
//学历
import selectDict from "@/packagePages/my/completeInfo/components/selectDict.vue";
//地址
import selectMarriage from "@/packagePages/my/completeInfo/selectMarriage.vue";
//家乡
import selectAddress from "@/packagePages/my/completeInfo/components/selectAddress.vue";
//体重
import selectWeight from "@/packagePages/my/completeInfo/components/selectWeight.vue";
//身高
import selectHeight from "@/packagePages/my/completeInfo/components/selectHeight.vue";
//性别
import selectSex from "@/packagePages/my/completeInfo/components/selectSex.vue";
//生日
import selectBirthday from "@/packagePages/my/completeInfo/components/selectBirthday.vue";
import useCounterStore from "@/stores/counter";
const counter = useCounterStore();
const { proxy } = getCurrentInstance();
const instance = getCurrentInstance();

let model = ref({
  nickName: "",
  sex: "MAN",
  birthday: "",
  logoId: "",
  constellation: "", //星座
  height: 150,
  school: "", //毕业学校
  edu: "", //学历
  homeEconomize: "",
  homeMarket: "",
  homeCounty: "",
  industry: "", //行业,
  income: "", //收入
  lifePhotosId: "", //生活照
  cityLon: "",
  cityLat: "",
  city: "",
  district: "",
  reallyName: "",
  idCard: "",
  oneselfTags: "", //自我标签
  hisTags: "", //对方期望标签
  oneselfDescribe: "", //自我描述
  hisDescribe: "", //期望对方描述
  weight: "", //体重
  marriage: 2, //婚姻状况
  foreignCompanyName: "",
});
let schools = ref([]); //学校
let loading = ref(false);

onLoad(() => {
  getInfo();
});

let birthday_format = computed(() => {
  if (model.value.birthday && model.value.constellation) {
    return `${model.value.birthday} | ${model.value.constellation}`;
  } else if (model.value.birthday) {
    return model.value.birthday;
  } else if (model.value.constellation) {
    return model.value.constellation;
  } else {
    return "";
  }
});

let sex_format = computed(() => {
  if (model.value.sex == "MAN") {
    return "男";
  } else if (model.value.sex == 1) {
    return "男";
  } else {
    return "女";
  }
});

let weight_format = computed(() => {
  let height = model.value.weight.toString();
  if (height.includes("kg")) return model.value.weight;
  return model.value.weight + "cm";
});

let height_format = computed(() => {
  let height = model.value.height.toString();
  if (height.includes("cm")) return model.value.height;
  return model.value.height + "cm";
});

let searchSchool = async (value) => {
  let data = await proxy.$apis.getSchoolName({
    type: model.value.edu,
    content: value,
  });
  schools.value = data;
};

let schoolClick = (e) => {
  model.value.school = e.name;
  schools.value = [];
};

async function getInfo() {
  await counter.getUserData();
  model.value = proxy.$utils.formatFunction(counter.slParams.userInfo, {
    ...model.value,
  });
  console.log("用户信息", model.value);
}

async function chooseLocation() {
  try {
    uni.showLoading({
      title: "获取当前定位中",
    });
    //let res = await proxy.$auth.chooseLocation();

    let resp;
    // #ifdef APP-PLUS
    let ret = await counter.permissionFunc("location");
    if (ret) resp = await counter.getPosition();
    // #endif

    // #ifndef APP-PLUS
    resp = await counter.getPosition();
    // #endif

    if (!resp) return;
    model.value.cityLon = resp.longitude;
    model.value.cityLat = resp.latitude;
    let address = await proxy.$atlas.getCityInfo(resp.longitude, resp.latitude);
    let { province, city, district } = address.addressComponent;
    if (
      proxy.$utils.isString(province) &&
      proxy.$utils.isString(city) &&
      province &&
      city
    ) {
      model.value.city = province;
      model.value.district = city;
    } else if (
      proxy.$utils.isString(province) &&
      proxy.$utils.isString(district) &&
      province &&
      district
    ) {
      model.value.city = province;
      model.value.district = district;
    }
  } catch (error) {
  } finally {
    uni.hideLoading();
  }
}

async function save() {
  try {
    loading.value = true;
    console.log("提交数据", model.value);
    await proxy.$apis.loginReginfo(model.value);
    getInfo();
    proxy.$utils.showToast("保存成功");
    proxy.$utils.back();
  } catch (error) {
  } finally {
    loading.value = false;
  }
}
</script>



<style lang="scss" scoped>
.container {
  min-height: 100vh;
  background-color: #fafafa;
  padding-bottom: 80px;
  .box {
    background-color: white;
    margin-bottom: 10px;
    padding-top: 10px;
    .title {
      position: relative;
      border-left: 3px solid #9e06ff;
      margin-left: 15px;
      padding-left: 5px;
    }
    .positionRelative {
      .school-list {
        position: absolute;
        max-height: 300px;
        overflow-y: auto;
        top: 30px;
        right: 0;
        background-color: white;
        padding-bottom: 20px;
        z-index: 1;
        .school {
          padding: 10px 10px 0 10px;
        }
      }
    }
  }
  .actionBtn {
    position: fixed;
    bottom: 25px;
    left: 0;
    right: 0;
    padding: 0 25px;
  }
}
</style>






